package com.ebelter.bpm.activitys;


import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.ebelter.bpm.R;
import com.ebelter.bpm.common.Constant;
import com.ebelter.btcomlib.bases.activity.BaseActivity;
import com.ebelter.btcomlib.models.https.NetUtils;
import com.ebelter.btcomlib.models.https.interfaces.HttpResponse;
import com.ebelter.btcomlib.models.https.responses.BaseResponse;

import com.ebelter.btcomlib.utils.StringUtils;
import com.ebelter.btcomlib.utils.ToastUtil;
import com.ebelter.btcomlib.utils.VerifyUtils;
import com.ebelter.btcomlib.utils.activity_manager.ActivityManagers;
import com.ebelter.btcomlib.utils.log.LogUtils;
import com.ebelter.btcomlib.views.CombinView1;


public class BPM_ModifyPswActivity extends BaseActivity {

    public static final String TAG = "BPM_VerificEmailCodeActivity";

    public static final String KEY_ACTION = "KEY_ACTION";
    public static final String KEY_CMD = "KEY_CMD";
    public static final String KEY_EMAIL = "KEY_EMAIL";
    public static final String KEY_EMAILCODE = "KEY_EMAILCODE";



    public static final int MODIFY_PSW = 1;


    String emailStr;
    String emailCodeStr;



    TextView topTitleTv;

    CombinView1 cb1_bpm_p1;
    CombinView1 cb1_bpm_p2;

    TextView emailcode_next_bt;


    @Override
    protected int loadLayoutById() {
        return R.layout.activity_bpm_modify_psw;
    }



    @Override
    protected void initView() {
        FV();



    }

    int CMD_WHICH;
    @Override
    protected void initData() {
        CMD_WHICH = getIntent().getIntExtra(KEY_CMD,0);

        if (CMD_WHICH!=Constant.IBpm.CMD_PHONE&&CMD_WHICH!=Constant.IBpm.CMD_EMAIL){
            throw new RuntimeException("参数不对");
        }
        emailStr = getIntent().getStringExtra(KEY_EMAIL);//emailStr可能是手机号码
        emailCodeStr = getIntent().getStringExtra(KEY_EMAILCODE);

        if (StringUtils.isEmpty(emailCodeStr)||StringUtils.isEmpty(emailStr)){
            throw new RuntimeException("非法参数");
        }

    }

    private void FV() {
        topTitleTv=findViewById(R.id.top_title_tv);
        cb1_bpm_p1 = findViewById(R.id.cb1_bpm_p1);
        cb1_bpm_p2 = findViewById(R.id.cb1_bpm_p2);


    }



    //返回键被按下
    public void backClick(View view){
        LogUtils.i(TAG,"返回键被按下");
        finish();
    }



    //完成按钮
    public void doneBtClick(View view){
        LogUtils.i(TAG,"doneBtClick");

        if (CMD_WHICH==Constant.IBpm.CMD_PHONE){
            phoneSetPsw();
        }else if (CMD_WHICH==Constant.IBpm.CMD_EMAIL){
            emailSetPsw();
        }
    }

    private void emailSetPsw() {
        if (TextUtils.isEmpty(emailStr)){
            ToastUtil.show(R.string.Email_must_not_be_empty);
            return;
        }
        if (!VerifyUtils.emailFormat(emailStr)){
            ToastUtil.show(R.string.Incorrect_email_format);
            return;
        }
        if (TextUtils.isEmpty(emailCodeStr)){
            ToastUtil.show(R.string.Mail_verification_code_cannot_be_empty);
            return;
        }

        String psw1 = cb1_bpm_p1.getTitle1();
        String psw2 = cb1_bpm_p2.getTitle1();
        if (TextUtils.isEmpty(psw1)){
            ToastUtil.show(R.string.The_password_cannot_be_empty);
            return;
        }
        if (!TextUtils.equals(psw1,psw2)){
            ToastUtil.show(R.string.The_two_passwords_are_inconsistent);
            return;
        }

        if (psw1.length()<6){
            ToastUtil.show(R.string.psw_less_tip);
            return;
        }
        if (psw1.length()>20){
            ToastUtil.show(R.string.psw_over_tip);
            return;
        }

        if (!NetUtils.available()){
            ToastUtil.show(R.string.Network_error);
            return;
        }

        NetUtils.getInstance().findPsw(this, emailStr, psw1, emailCodeStr, new HttpResponse<BaseResponse>() {
            @Override
            public void result(boolean isRequestSuccess, String requestDes, BaseResponse baseResponse, String s) {
                if (!isRequestSuccess){
                    ToastUtil.show(R.string.Network_connection_error);
                    return;
                }
                if (baseResponse.resultCode==0){
                    ToastUtil.show(R.string.Modify_Success);
                    ActivityManagers.getInstance().closeActivity(BPM_VerificEmailCodeActivity.TAG);//关闭
                    finish();
                }else if (baseResponse.resultCode==117){
                    ToastUtil.show(R.string.Email_is_not_registered);
                }else if (baseResponse.resultCode==106){
                    ToastUtil.show(R.string.Email_verification_code_has_expired);

                }else if (baseResponse.resultCode==107){
                    ToastUtil.show(R.string.Wrong_email_verification_code);
                }else if(baseResponse.resultCode==110){
                    ToastUtil.show(R.string.An_exception_occurred_on_the_server);
                }else {
                    ToastUtil.show(R.string.Modify_Failed);
                }

            }
        });

    }

    private void phoneSetPsw() {

        if (TextUtils.isEmpty(emailStr)){
            ToastUtil.show("手机号码不能为空");
            return;
        }
        if (!VerifyUtils.isMobile(emailStr)){
            ToastUtil.show("请输入正确的手机号码");
            return;
        }
        if (TextUtils.isEmpty(emailCodeStr)){
            ToastUtil.show("请输入手机验证码");
            return;
        }


        String psw1 = cb1_bpm_p1.getTitle1();
        String psw2 = cb1_bpm_p2.getTitle1();
        if (TextUtils.isEmpty(psw1)){
            ToastUtil.show(R.string.The_password_cannot_be_empty);
            return;
        }
        if (!TextUtils.equals(psw1,psw2)){
            ToastUtil.show(R.string.The_two_passwords_are_inconsistent);
            return;
        }

        if (psw1.length()<6){
            ToastUtil.show(R.string.psw_less_tip);
            return;
        }
        if (psw1.length()>20){
            ToastUtil.show(R.string.psw_over_tip);
            return;
        }

        if (!NetUtils.available()){
            ToastUtil.show(R.string.Network_error);
            return;
        }

        NetUtils.getInstance().findPswPhone(this, emailStr, psw1, emailCodeStr, new HttpResponse<BaseResponse>() {
            @Override
            public void result(boolean isRequestSuccess, String requestDes, BaseResponse baseResponse, String s) {
                if (!isRequestSuccess){
                    ToastUtil.show(R.string.Network_connection_error);
                    return;
                }
                if (baseResponse.resultCode==0){
                    ToastUtil.show(R.string.Modify_Success);
                    ActivityManagers.getInstance().closeActivity(BPM_VerificEmailCodeActivity.TAG);//关闭
                    finish();
                }else if (baseResponse.resultCode==117){
                    ToastUtil.show("手机号码没有注册");
                }else if (baseResponse.resultCode==106){
                    ToastUtil.show("手机验证码已失效");

                }else if (baseResponse.resultCode==107){
                    ToastUtil.show("错误的手机验证码");
                }else if(baseResponse.resultCode==110){
                    ToastUtil.show(R.string.An_exception_occurred_on_the_server);
                }else {
                    ToastUtil.show(R.string.Modify_Failed);
                }

            }
        });

    }

}
